@extends('layouts.header')
@section('title','列表')
@section('content')
    <nav class="breadcrumb"><i class="Hui-iconfont">&#xe67f;</i> 首页 <span class="c-gray en">&gt;</span> 设备管理 <span class="c-gray en">&gt;</span>工位编码<a class="btn btn-primary radius r" style="line-height:1.6em;margin-top:3px" href="javascript:location.replace(location.href);" title="刷新"><i class="Hui-iconfont">&#xe68f;</i></a>
    </nav>
    <div class="page-container">

        <div class="cl pd-5 bg-1 bk-gray">
            <span class="">
                <a href="javascript:;" onclick="layer_show('添加','/{{config('constant.name_admin')}}/vcr/add?id=0')" class="btn btn-primary radius"><i class="Hui-iconfont">&#xe600;</i> 添加</a>
            </span>
        </div>
        <div class="mt-20">
            <table class="table table-border table-bordered table-hover table-bg" id="datatable">
                <thead>
                <tr class="text-c">
                    {{-- <th width="25"><input type="checkbox" name="" value=""></th>
                    --}}
                    <th>名称</th>
                    <th>设备序列号</th>
                    <th>加密验证码</th>
                    <th>通道数量</th>
                    <th>局域网IP</th>
                    <th>状态</th>
                    <th>创建时间</th>
                    <th>操作</th>
                </tr>
                </thead>
            </table>
        </div>
    </div>

    <script type="text/javascript">
        var table;
        $(function () {
            $.fn.dataTable.ext.errMode = 'throw'; //datatable抛异常的方式，在console中输出，默认以alert的方式弹出
            table = $('#datatable').DataTable({
                "deferRender": true, //延迟渲染速度
                "ordering": false, // 是否排序
                ajax: "/{{ config('constant.name_admin') }}/vcr/index",
                "processing": true, //加载数据时候是否显示进度条
                language: {
                    url: '/admin/lib/datatables/dataTables_Chinese_Lang.json'
                },
                columns: [
                    {data: "name", class: "text-c node-name"},
                    {data: "serial", class: "text-c serial"},
                    {data: "valid_code", class: "text-c valid_code"},
                    {data: "channel_count", class: "text-c channel_count"},
                    {data: "lan_ip", class: "text-c lan_ip"},
                    {
                        data: "ishide", class: "text-c ishide", render: function (data, type, row, meta) {
                            switch (data) {
                                case 0:
                                    return "<span class='badge badge-success'>显示</span>";
                                    break;
                                case 1:
                                    return "<span class='badge badge-default'>隐藏</span>";
                                    break;
                            }

                        }
                    },
                    {data: "ctime", class: "text-c ctime"},
                    {
                        data: null,
                        class: "text-c",
                        //操作
                        render: function (data, type, row, meta) {
                            return '<a class="ml-5 btn btn-primary-outline radius size-MINI" href="javascript:;" onclick="edit(\'修改\',\'/{{ config('constant.name_admin') }}/vcr/edit\',\'' + row.id + '\')">修改</a>' +
                                '<a onclick="del(' + row.id + ',this)" class="ml-5 btn btn-danger-outline radius size-MINI">删除</a>'+ 
                                '<a onclick="layer_show(`预览`, `live?num=1&id=' + row.id + '`)" class="ml-5 btn btn-secondary-outline radius size-MINI">内网预览</a>'; 
                                // '<a onclick="syncDev(this)" class="ml-5 btn btn-secondary-outline radius size-MINI">同步设备</a>';
                        }
                    }
                ],
            });

        });

        function syncDev(obj) {
            let param = {
                serial: $(obj).closest('tr').find('td.serial').text(),
                valid_code: $(obj).closest('tr').find('td.valid_code').text(),
                _form_token : '{{ csrf_form_token() }}'
            };
            ajax('post', 'ysyAdd', param);
        }

        function liveShow(url) {
            $.ajax({
                type: "GET",
                cache: false,
                url: url,
                dataType: "jsonp",
                timeout: 2000,
                complete: function (res) {
                    res.status == 200 ? layer_show('主机屏幕', url) : '';
                }
            }).fail(function (jqXHR, textStatus, errorThrown) {
                // net::ERR_CONNECTION_REFUSED 发生时，也能进入
                layer.msg("主机未开启或未共享");
            });
        }

        /*重复输出一个字符串*/
        function str_repeat(string, times) {
            var res = "";
            for (var i = 0; i < times; i++) {
                res += string;
            }
            return res;
        }

        /*表格重新加载*/
        function table_reload(bool) {
            //清除表头 CheckBox的对勾
            $("#datatable").find("thead").find("input[type=checkbox]").removeAttr('checked');
            if (bool) {
                //true时，跳到第一页
                table.ajax.reload();
            } else {
                //false时，停留在原来页
                table.ajax.reload(null, false);
            }
        }

        /*
        参数解释：
        title	标题
        url		请求的url
        id		需要操作的数据id
        w		弹出层宽度（缺省调默认值）
        h		弹出层高度（缺省调默认值）
        */

        /*编辑*/
        function edit(title, url, id, w, h) {
            layer_show(title, url + '?id=' + id, w, h);
        }

        /*删除*/
        function del(id, obj) {
            var index2;
            var node_name = $(obj).closest('tr').find('td.node-name').text();
            layer.confirm('确认要删除“' + node_name + '”吗？', function (index) {
                $.ajax({
                    type: 'GET',
                    url: '/{{config('constant.name_admin')}}/vcr/del' + '?id=' + id,
                    // dataType: 'json',
                    success: function (data) {
                        if (data === '1') {
                            table_reload(0); //0停留在当前页，1回到第一页
                            layer.msg('删除成功!', {
                                icon: 1,
                                time: 2000
                            });
                        }
                    },
                    error: function (data) {
                        ajax_error(data);
                    },
                    beforeSend: function () {
                        //0.4透明度 白色遮罩
                        index2 = layer.load(2, {
                            shade: [0.05, '#000']
                        });
                    },
                    complete: function () {
                        layer.close(index2);
                    },
                });
            });
        }

    </script>
@endsection
